﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYFamilyLibDataManager;

namespace YArchitech.Plumbing
{
	public partial class FormBathroomLayout : System.Windows.Forms.Form
	{
		public FormBathroomLayout(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Icon = Resource.YArchitechIcon;
			this.AddGridColumn("Name", "卫浴名称", "TName", 150, this.dataGridViewFamilf);
			this.InitFromData();
		}

		private void InitFromData()
		{
			UserSetting userSetting = new UserSetting();
			string text = userSetting.GetValue("FormBathroomLayout", "BathroomType");
			string value = userSetting.GetValue("FormBathroomLayout", "FamilyName");
			if (string.IsNullOrEmpty(text))
			{
				text = "1";
			}
			this.ChangeBtnType(Convert.ToInt32(text), value);
			text = userSetting.GetValue("FormBathroomLayout", "textBoxHeigh");
			if (string.IsNullOrEmpty(text))
			{
				text = "0.0";
			}
			this.textBoxHeigh.Text = text;
			text = userSetting.GetValue("FormBathroomLayout", "checkAngel");
			this.checkAngel.Checked = text.Equals(true.ToString());
			this.checkAngel_CheckedChanged(null, null);
			text = userSetting.GetValue("FormBathroomLayout", "textBoxAngel");
			if (string.IsNullOrEmpty(text))
			{
				text = "0";
			}
			this.textBoxAngel.Text = text;
			this.rad_LayoutDist.Checked = (1 == Convert.ToInt32(userSetting.GetValue("FormBathroomLayout", "alongType")));
			text = userSetting.GetValue("FormBathroomLayout", "textBox_LayoutNum");
			if (string.IsNullOrEmpty(text))
			{
				text = "0.5";
			}
			this.textBox_LayoutNum.Text = text;
			text = userSetting.GetValue("FormBathroomLayout", "textBox_LayoutDis");
			if (string.IsNullOrEmpty(text))
			{
				text = "400";
			}
			this.textBox_LayoutDis.Text = text;
			text = userSetting.GetValue("FormBathroomLayout", "label_Layout_Num");
			if (string.IsNullOrEmpty(text))
			{
				text = "2";
			}
			this.textBox_LayoutTwo.Text = text;
			text = userSetting.GetValue("FormBathroomLayout", "label_Layout_Dis");
			if (string.IsNullOrEmpty(text))
			{
				text = "800";
			}
			this.textBox_Layout.Text = text;
			if (this.rad_LayoutNum.Checked)
			{
				this.textBox_LayoutNum.Visible = true;
				this.textBox_LayoutDis.Visible = false;
				this.textBox_Layout.Visible = false;
				this.textBox_LayoutTwo.Visible = true;
				return;
			}
			this.textBox_LayoutNum.Visible = false;
			this.textBox_LayoutDis.Visible = true;
			this.textBox_Layout.Visible = true;
			this.textBox_LayoutTwo.Visible = false;
		}

		private void SaveFromData()
		{
			UserSetting userSetting = new UserSetting();
			userSetting.WriteValue("FormBathroomLayout", "BathroomType", this.nBathroomType.ToString());
			string text = this.dataGridViewFamilf.Rows[this.dataGridViewFamilf.SelectedCells[0].RowIndex].Cells["TName"].Value.ToString();
			userSetting.WriteValue("FormBathroomLayout", "FamilyName", text);
			text = this.textBoxHeigh.Text;
			userSetting.WriteValue("FormBathroomLayout", "textBoxHeigh", text);
			userSetting.WriteValue("FormBathroomLayout", "checkAngel", this.checkAngel.Checked.ToString());
			text = this.textBoxAngel.Text;
			userSetting.WriteValue("FormBathroomLayout", "textBoxAngel", text);
			if (this.rad_LayoutNum.Checked)
			{
				userSetting.WriteValue("FormBathroomLayout", "alongType", "0");
				text = this.textBox_LayoutTwo.Text;
				userSetting.WriteValue("FormBathroomLayout", "label_Layout_Num", text);
				return;
			}
			userSetting.WriteValue("FormBathroomLayout", "alongType", "1");
			text = this.textBox_Layout.Text;
			userSetting.WriteValue("FormBathroomLayout", "label_Layout_Dis", text);
		}

		public void GetBathroomData(int nTypeIndex, string szFamily)
		{
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			List<ListItem> allName = spaceFamilyDataReader.GetAllName(this.BathroomDataName, nTypeIndex);
			this.dataGridViewFamilf.DataSource = allName;
			this.dataGridViewFamilf.Columns["Value"].Visible = false;
			this.dataGridViewFamilf.Columns["Tag"].Visible = false;
			this.dataGridViewFamilf.RowHeadersVisible = false;
			if (this.dataGridViewFamilf.Rows != null && this.dataGridViewFamilf.Rows.Count > 0)
			{
				int index = 0;
				if (!string.IsNullOrEmpty(szFamily))
				{
					for (int i = 0; i < this.dataGridViewFamilf.Rows.Count; i++)
					{
						if (this.dataGridViewFamilf.Rows[i].Cells["TName"].Value.ToString() == szFamily)
						{
							index = i;
						}
					}
				}
				this.dataGridViewFamilf.Rows[index].Selected = true;
				this.dataGridViewFamilf.CurrentCell = this.dataGridViewFamilf.Rows[index].Cells["TName"];
			}
			if (this.dataGridViewFamilf.SelectedRows.Count > 0 && this.dataGridViewFamilf.SelectedRows[0].Cells["Tag"] != null)
			{
				foreach (ListItem listItem in spaceFamilyDataReader.GetInfoById(this.BathroomDataName, int.Parse(this.dataGridViewFamilf.SelectedRows[0].Cells["Tag"].Value.ToString())))
				{
					if (listItem.Name == "Image")
					{
						if (File.Exists(listItem.Value))
						{
							this.pictureBoxShow.Image = new Bitmap(listItem.Value);
						}
						else
						{
							this.pictureBoxShow.Image = null;
						}
					}
					else if (listItem.Name == "FamilySymbol")
					{
						this.UptadaFamilySybol(listItem.Value);
					}
				}
			}
		}

		private void UptadaFamilySybol(string szData)
		{
			this.listBoxFamilySybol.Items.Clear();
			foreach (string text in szData.Split(new char[]
			{
				';',
				',',
				'，'
			}))
			{
				if (text.Trim() != "")
				{
					this.listBoxFamilySybol.Items.Add(text);
				}
			}
			if (this.listBoxFamilySybol.Items != null && this.listBoxFamilySybol.Items.Count > 0)
			{
				this.listBoxFamilySybol.SelectedIndex = 0;
			}
		}

		private void AddGridColumn(string dataPropertyName, string headerText, string name, int width, DataGridView dgv)
		{
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.DataPropertyName = dataPropertyName;
			dataGridViewTextBoxColumn.HeaderText = headerText;
			dataGridViewTextBoxColumn.Name = name;
			dataGridViewTextBoxColumn.Width = width;
			dgv.Columns.AddRange(new DataGridViewColumn[]
			{
				dataGridViewTextBoxColumn
			});
		}

		private void AlongTypeChanged(object sender, EventArgs e)
		{
			int tabIndex = (sender as RadioButton).TabIndex;
			if (this.rad_LayoutNum.Checked)
			{
				this.label_Layout.Text = "个数：";
				this.textBox_LayoutNum.Visible = true;
				this.textBox_LayoutDis.Visible = false;
				this.label_LayoutTwo.Text = "首个卫浴与起点的距离\n和卫浴间距比值：";
				this.textBox_Layout.Visible = false;
				this.textBox_LayoutTwo.Visible = true;
				return;
			}
			this.label_Layout.Text = "间距(mm)：";
			this.textBox_LayoutNum.Visible = false;
			this.textBox_LayoutDis.Visible = true;
			this.label_LayoutTwo.Text = "首个卫浴与起点的距离\n(mm)：";
			this.textBox_Layout.Visible = true;
			this.textBox_LayoutTwo.Visible = false;
		}

		private void dataGridViewFamilf_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
		{
		}

		private void dataGridViewFamilf_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex < 0)
			{
				return;
			}
			foreach (ListItem listItem in new SpaceFamilyDataReader().GetInfoById(this.BathroomDataName, int.Parse(this.dataGridViewFamilf.Rows[e.RowIndex].Cells["Tag"].Value.ToString())))
			{
				if (listItem.Name == "Image")
				{
					if (File.Exists(listItem.Value))
					{
						this.pictureBoxShow.Image = new Bitmap(listItem.Value);
					}
					else
					{
						this.pictureBoxShow.Image = null;
					}
				}
				else if (listItem.Name == "FamilySymbol")
				{
					this.UptadaFamilySybol(listItem.Value);
				}
			}
		}

		private bool CheckData(int nDrawType)
		{
			if (this.dataGridViewFamilf.SelectedCells == null || this.dataGridViewFamilf.SelectedCells.Count <= 0)
			{
				YJKMessageBox.Show("请选择要布置的卫浴种类！");
				return false;
			}
			if (this.listBoxFamilySybol.SelectedItems == null || this.listBoxFamilySybol.SelectedItems.Count <= 0)
			{
				YJKMessageBox.Show("请选择要布置的卫浴类型！");
				return false;
			}
			if (string.IsNullOrEmpty(this.textBoxHeigh.Text))
			{
				YJKMessageBox.Show("相对标高不能为空！");
				return false;
			}
			this.plumbingFixturesPara.OffSet = Convert.ToDouble(this.textBoxHeigh.Text);
			if (this.checkAngel.Checked)
			{
				if (string.IsNullOrEmpty(this.textBoxAngel.Text) || Convert.ToInt32(this.textBoxAngel.Text) < 0 || Convert.ToInt32(this.textBoxAngel.Text) > 360)
				{
					YJKMessageBox.Show("旋转角度应在0°~360°之间！");
					return false;
				}
				this.plumbingFixturesPara.Angle = Convert.ToDouble(this.textBoxAngel.Text);
			}
			else
			{
				this.plumbingFixturesPara.Angle = 0.0;
			}
			if (nDrawType == 0)
			{
				return true;
			}
			if (this.rad_LayoutNum.Checked)
			{
				this.LineParameter.LimitFlag = 1;
				if (string.IsNullOrEmpty(this.textBox_LayoutTwo.Text) || Convert.ToInt32(this.textBox_LayoutTwo.Text) < 1)
				{
					YJKMessageBox.Show("卫浴个数应大于0！");
					return false;
				}
				if (string.IsNullOrEmpty(this.textBox_LayoutNum.Text))
				{
					YJKMessageBox.Show("间距比值不能为空！");
					return false;
				}
				this.LineParameter.Count = Convert.ToInt32(this.textBox_LayoutTwo.Text);
				this.LineParameter.Ratio = Convert.ToDouble(this.textBox_LayoutNum.Text);
			}
			else
			{
				this.LineParameter.LimitFlag = 2;
				if (string.IsNullOrEmpty(this.textBox_Layout.Text) || Convert.ToInt32(this.textBox_Layout.Text) < 100)
				{
					YJKMessageBox.Show("卫浴间距不应小于100mm！");
					return false;
				}
				if (string.IsNullOrEmpty(this.textBox_LayoutDis.Text))
				{
					YJKMessageBox.Show("首个卫浴与起点距离不能为空！");
					return false;
				}
				this.LineParameter.Distance = (double)Convert.ToInt32(this.textBox_Layout.Text);
				this.LineParameter.DistanceSide = (double)Convert.ToInt32(this.textBox_LayoutDis.Text);
			}
			return true;
		}

		private void FamilyLoad()
		{
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			BuiltInCategory builtInCate = (BuiltInCategory)(-2001160);
			if (this.nBathroomType == 7)
			{
				builtInCate = (BuiltInCategory)(-2008055);
			}
			this.fs = spaceFamilyDataReader.GetFamilySymbolById(int.Parse(this.dataGridViewFamilf.Rows[this.dataGridViewFamilf.SelectedCells[0].RowIndex].Cells["Tag"].Value.ToString()), this.BathroomDataName, this.listBoxFamilySybol.SelectedItems[0].ToString(), this.doc, builtInCate);
		}

		private void btnSingle_Click(object sender, EventArgs e)
		{
			this.layTerminalType = 0;
			if (!this.CheckData(this.layTerminalType))
			{
				return;
			}
			this.SaveFromData();
			this.FamilyLoad();
			base.DialogResult = DialogResult.OK;
		}

		private void btnAlong_Click(object sender, EventArgs e)
		{
			this.layTerminalType = 1;
			if (!this.CheckData(this.layTerminalType))
			{
				return;
			}
			this.SaveFromData();
			this.FamilyLoad();
			base.DialogResult = DialogResult.OK;
		}

		private void BtnTypeChanged(object sender, EventArgs e)
		{
			int tabIndex = (sender as Button).TabIndex;
			this.ChangeBtnType(tabIndex, null);
		}

		private void ChangeBtnType(int nTabInedex, string szFamily = null)
		{
			if (nTabInedex < 1 || nTabInedex > 7)
			{
				nTabInedex = 1;
			}
			this.nBathroomType = nTabInedex;
			this.button1.Image = new Bitmap(this.szbtn1);
			this.button2.Image = new Bitmap(this.szbtn2);
			this.button3.Image = new Bitmap(this.szbtn3);
			this.button4.Image = new Bitmap(this.szbtn4);
			this.button5.Image = new Bitmap(this.szbtn5);
			this.button6.Image = new Bitmap(this.szbtn6);
			this.button7.Image = new Bitmap(this.szbtn7);
			switch (nTabInedex)
			{
			case 1:
				this.button1.Image = new Bitmap(this.szbtnSel1);
				break;
			case 2:
				this.button2.Image = new Bitmap(this.szbtnSel2);
				break;
			case 3:
				this.button3.Image = new Bitmap(this.szbtnSel3);
				break;
			case 4:
				this.button4.Image = new Bitmap(this.szbtnSel4);
				break;
			case 5:
				this.button5.Image = new Bitmap(this.szbtnSel5);
				break;
			case 6:
				this.button6.Image = new Bitmap(this.szbtnSel6);
				break;
			case 7:
				this.button7.Image = new Bitmap(this.szbtnSel7);
				break;
			}
			this.GetBathroomData(nTabInedex, szFamily);
		}

		private void btn_MouseEnter(object sender, EventArgs e)
		{
			Button button = sender as Button;
			ToolTip toolTip = new ToolTip();
			toolTip.ShowAlways = true;
			switch (button.TabIndex)
			{
			case 1:
				toolTip.SetToolTip(button, "大便器");
				return;
			case 2:
				toolTip.SetToolTip(button, "小便器");
				return;
			case 3:
				toolTip.SetToolTip(button, "洗脸盆");
				return;
			case 4:
				toolTip.SetToolTip(button, "洗涤盆及拖布池");
				return;
			case 5:
				toolTip.SetToolTip(button, "浴缸");
				return;
			case 6:
				toolTip.SetToolTip(button, "沐浴喷头");
				return;
			case 7:
				toolTip.SetToolTip(button, "地漏");
				return;
			default:
				return;
			}
		}

		private void buttonAngel_Click(object sender, EventArgs e)
		{
			ViewPlan viewPlan = this.doc.ActiveView as ViewPlan;
			if (viewPlan == null || (int)viewPlan.ViewType != 1)
			{
				YJKMessageBox.Show("手动测量只能在平面视图下可用！");
				return;
			}
			base.DialogResult = DialogResult.Abort;
		}

		public void SetAngelFromPoint(XYZ startPnt, XYZ endPnt)
		{
			double num = 0.0;
			XYZ xyz = new XYZ(startPnt.X, startPnt.Y, 0.0);
			XYZ xyz2 = new XYZ(endPnt.X, endPnt.Y, 0.0).Subtract(xyz).Normalize();
			if (Math.Abs(xyz2.X) < 0.0001)
			{
				if (xyz2.Y > 0.0)
				{
					num = 90.0;
				}
				if (xyz2.Y < 0.0)
				{
					num = 270.0;
				}
				if (xyz2.Y == 0.0)
				{
					num = 0.0;
				}
			}
			if (Math.Abs(xyz2.Y) < 0.0001)
			{
				if (xyz2.X == 0.0)
				{
					num = 0.0;
				}
				if (xyz2.X > 0.0)
				{
					num = 0.0;
				}
				if (xyz2.X < 0.0)
				{
					num = 180.0;
				}
			}
			if ((xyz2.X > 0.0 && xyz2.Y > 0.0) || (xyz2.X < 0.0 && xyz2.Y > 0.0))
			{
				num = Convert.ToDouble(StringTools.CutOffEfficacious((new XYZ(1.0, 0.0, 0.0).AngleTo(xyz2) * 180.0 / Math.PI).ToString(), 2));
			}
			if ((xyz2.X > 0.0 && xyz2.Y < 0.0) || (xyz2.X < 0.0 && xyz2.Y < 0.0))
			{
				num = new XYZ(1.0, 0.0, 0.0).AngleTo(xyz2) * 180.0 / Math.PI;
				num = Convert.ToDouble(StringTools.CutOffEfficacious((360.0 - num).ToString(), 2));
			}
			if (num == 360.0)
			{
				num = 0.0;
			}
			num = Convert.ToDouble(StringTools.CutOffEfficacious(num.ToString(), 0));
			this.textBoxAngel.Text = num.ToString();
		}

		private void checkAngel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.checkAngel.Checked)
			{
				this.textBoxAngel.Enabled = true;
				this.buttonAngel.Enabled = true;
				return;
			}
			this.textBoxAngel.Enabled = false;
			this.buttonAngel.Enabled = false;
		}

		private void button8_Click(object sender, EventArgs e)
		{
			if (FamilyUtil.PathIni.ReadPath(this.doc.Application.VersionNumber) == "")
			{
				YJKMessageBox.Show("请先安装盈建科族立得。");
				return;
			}
			FormBathroomMore formBathroomMore = new FormBathroomMore(new PlumbingDataReader(this.doc).GetFamilyAllInfoTree("卫浴"), this.doc);
			if (formBathroomMore.ShowDialog() == DialogResult.OK)
			{
				this.fs = formBathroomMore.fs;
				this.plumbingFixturesPara = formBathroomMore.plumbingFixturesPara;
				this.LineParameter = formBathroomMore.LineParameter;
				this.layTerminalType = formBathroomMore.layTerminalType;
				base.DialogResult = DialogResult.OK;
			}
		}

		private string BathroomDataName = "EquipmentSanitary";

		private Document doc;

		public FamilySymbol fs;

		private int nBathroomType;

		public PlumbingFixturesParam plumbingFixturesPara = new PlumbingFixturesParam();

		public LayLineParameter LineParameter = new LayLineParameter();

		public int layTerminalType;

		private readonly string szbtn1 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btn1.bmp");

		private readonly string szbtnSel1 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btnSel1.bmp");

		private readonly string szbtn2 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btn2.bmp");

		private readonly string szbtnSel2 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btnSel2.bmp");

		private readonly string szbtn3 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btn3.bmp");

		private readonly string szbtnSel3 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btnSel3.bmp");

		private readonly string szbtn4 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btn4.bmp");

		private readonly string szbtnSel4 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btnSel4.bmp");

		private readonly string szbtn5 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btn5.bmp");

		private readonly string szbtnSel5 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btnSel5.bmp");

		private readonly string szbtn6 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btn6.bmp");

		private readonly string szbtnSel6 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btnSel6.bmp");

		private readonly string szbtn7 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btn7.bmp");

		private readonly string szbtnSel7 = Path.Combine(Product.InstallLocation, "EquipmentImage", "FormBathroomLayout", "btnSel7.bmp");
	}
}
